package main

import (
	"gin-demo/controller"
	"gin-demo/middlewares"
	"gin-demo/tool"
	"log"
	"net/http"

	"github.com/gin-gonic/gin"
	_ "github.com/go-sql-driver/mysql"
)

// 注册路由
func registerRouter(router *gin.Engine) {
	new(controller.HelloController).Router(router)
	new(controller.MemberController).Router(router)
	new(controller.FoodCategoryController).Router(router)
	new(controller.ShopController).Router(router)
}

func main() {
	log.Println("Starting server...")

	cfg, err := tool.ParseConfig("./config/app.json")
	if err != nil {
		panic(err)
	}

	_, err = tool.OrmEngine(cfg)
	if err != nil {
		log.Fatalln(err.Error())
		return
	}

	_, err = tool.InitMongodbStore()
	if err != nil {
		log.Fatalln(err.Error())
		return
	}

	app := gin.Default()
	// 配置一个静态文件路径,(api, 文件夹路径)
	app.StaticFS("/static", http.Dir("./static"))

	// 设置全局跨域, 必须在使用路由前进行设置，否则会导致不生效
	app.Use(middlewares.Cors())

	// 集成 session
	tool.InitSessionStore(app)

	// 注册路由
	registerRouter(app)

	if err := app.Run(cfg.AppHost + ":" + cfg.AppPort); err != nil {
		log.Fatalln(err.Error())
	}
}
